<template>
    <el-dialog v-model="visible" :title="!dataForm.id ? '新增' : '修改' " :close-on-click-modal="false"
               :close-on-press-escape="false" :fullscreen="true">
        <el-form v-model="dataForm" :rules="rules" ref="dataForm" @keyup.enter.native="dataFormSubmitHandle()"
                 label-width="120px">
            <!--<el-form-item prop="id" label="编号">
                <el-input v-model="dataForm.id" placeholder="编号"></el-input>
            </el-form-item>-->
            <el-form-item prop="companyId" label="公司ID">
                <el-input v-model="dataForm.companyId" placeholder="公司ID"></el-input>
            </el-form-item>
            <el-form-item prop="companyName" label="公司名称">
                <el-input v-model="dataForm.companyName" placeholder="公司名称"></el-input>
            </el-form-item>
            <el-form-item prop="customer" label="联系人">
                <el-input v-model="dataForm.customer" placeholder="联系人"></el-input>
            </el-form-item>
            <el-form-item prop="phone" label="电话">
                <el-input v-model="dataForm.phone" placeholder="电话"></el-input>
            </el-form-item>
            <el-form-item prop="address" label="联系地址">
                <el-input v-model="dataForm.address" placeholder="联系地址"></el-input>
            </el-form-item>
        </el-form>
        <template #footer>
            <el-button @click="visible = false">取消</el-button>
            <el-button type="primary" @click="dataFormSubmitHandle()">确定</el-button>
        </template>
    </el-dialog>
</template>

<script>
    import {request} from "@/network/request";
    import {isMobile} from "@/utils/validate"
    import {reactive} from 'vue'

    export default {
        name: "CustomerAddOrUpdateComponent",
        emits: ["refreshDataList"],
        setup(){
            const  rules =reactive({
                checkPhone:[{
                    validator:isMobile,
                    trigger:'blur'
                }]
            })
            return{
                rules
            }
        },

        data: function () {
            return {
                visible: false,
                dataForm: {
                    id: '',
                    companyId:'',
                    companyName:'',
                    customer:'',
                    phone:'',
                    address:''

                }
            }
        },
        beforeCreate: function () {
            self = this;
        },
        methods: {
            init: function () {
                self.visible = true;
                self.$nextTick(function () {
                    self.$refs['dataForm'].resetFields();
                    self.dataForm.companyId ='' ;
                    self.dataForm.companyName ='' ;
                    self.dataForm.customer ='' ;
                    self.dataForm.phone ='' ;
                    self.dataForm.address ='' ;

                    Promise.all([
                    ]).then(function () {
                        if (self.dataForm.id) {
                            self.getInfo();
                        }
                    });
                });
            },

            getInfo: function () {
                request({
                    url:'/sys/customer/'+self.dataForm.id.id,
                    method:'get'
                    }).then(function (res) {
                    if (res.code !== 0) {
                        return self.$message.error(res.msg);
                    }
                    self.dataForm = _.assignIn({}, self.dataForm, res.data);
                    /*self.dataForm.menuIdList.forEach(function (item) {
                        self.$refs.menuListTree.setChecked(item, true);
                    });
                    self.$refs.deptListTree.setCheckedKeys(self.dataForm.deptIdList);*/
                })},

            // 表单提交
            dataFormSubmitHandle: function () {
                if(self.dataForm.id){
                request({
                    url:"/sys/customer",
                    method:'put' ,
                    data: self.dataForm,

                })
               .then(function (res) {
                    if (res.code !== 0) {
                        return self.$message.error(res.msg);
                    }
                    self.$message({
                        message: '操作成功',
                        type: 'success',
                        duration: 500,
                        onClose: function () {
                            self.visible = false
                            self.$emit('refresh-data-list')
                        }
                    })

                })

                }else{request({
                    url:"/sys/customer",
                    method:'post' ,
                    data: self.dataForm,
                    params: self.dataForm,
                })
                    .then(function (res) {
                        if (res.code !== 0) {
                            return self.$message.error(res.msg);
                        }
                        self.$message({
                            message: '操作成功',
                            type: 'success',
                            duration: 500,
                            onClose: function () {
                                self.visible = false
                                self.$emit('refresh-data-list')
                            }
                        })

                    })

            }

            }}}

    /*self.$http.post('/sys/customer',self.dataForm).then(function (res) {
            if (res.data.code !== 0) {
                return self.$message.error(res.data.msg);
            }
            self.$message({
                message: self.$t('prompt.success'),
                type: 'success',
                duration: 500,
                onClose: function () {
                    self.visible = false
                    self.$emit('refresh-data-list')
                }
            })
        }).catch(function () {});*/
</script>

<style scoped>

</style>
